<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .changeimg{
            margin: 20px 25% ;
            /* position: relative; */
            text-align: center;
            background-color: antiquewhite;
            border: 10px solid antiquewhite;
        }
        img{
            width: 100%;
            height: auto;
        }
        button{
            text-align: center;
        }
    </style>
</head>
<body>
    <div class="changeimg">
        <p id="demo"></p>
        <img src="./image/01.jpg" alt="">
        <button id="up">上一张</button>
        <button id="down">下一张</button>
    </div>
    <script>
        var imgArr = ["./image/01.jpg","./image/02.jpg","./image/03.jpg","./image/04.jpg","./image/05.jpg"];
        //用户浏览网页时触发。
        window.onpageshow=function(){
            document.getElementById("demo").innerHTML=("共"+imgArr.length+"张，当前是第1张");
        };
        //页面加载完成后触发
        window.onload=function(){
            //获取id
            var up = document.getElementById("up");
            var down = document.getElementById("down");
            var img = document.getElementsByTagName("img")[0];
            // var imgArr = ["./image/01.jpg","./image/02.jpg","./image/03.jpg","./image/04.jpg","./image/05.jpg"];
            
            var i = 0;

            up.onclick = function(){
                i--  ;
                if (i<0) {
                    i=imgArr.length-1;
                   
                }
                img.src=imgArr[i];
                document.getElementById("demo").innerHTML=("共"+imgArr.length+"张，当前是第"+(i+1)+"张");
            }

           down.onclick=function(){
                i++;
                if (i>4) {
                    i=0;
                }
                img.src=imgArr[i];
                document.getElementById("demo").innerHTML=("共"+imgArr.length+"张，当前是第"+(i+1)+"张");
           }


        };
    </script>
</body>
</html>